package cn.mystyle.user.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.mystyle.common.bean.BaseController;
import cn.mystyle.common.bean.BaseResponse;
import cn.mystyle.common.response.ListResponse;
import cn.mystyle.model.user.UserInfo;
import cn.mystyle.user.server.service.UserService;

/**
 * @author clark
 * 用户控制器
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController
{
    // private Logger logger = LoggerFactory.getLogger(UserController.class);
    
    /**
     * 用户业务类
     */
    @Autowired
    private UserService userService;
    
    /**
     * 获取用户
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param jsonData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public String getUser(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        UserInfo user = JSON.parseObject(jsonData, UserInfo.class);
        List<UserInfo> result = userService.getUser(user);
        
        ListResponse<UserInfo> resp = new ListResponse<UserInfo>();
        resp.setResult(result);
        resp.setTotal(userService.getUserTotal(user));
        
        return resp.toString();
    }
    
    /**
     * 注册用户
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param jsonData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        UserInfo user = JSON.parseObject(jsonData, UserInfo.class);
        BaseResponse br = userService.insertUser(user);
        return br.toString();
    }
}
